package com.rxyb.redis.utils;

import com.rxyb.redis.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis set类型工具类
 *
 * @author lcf
 * @date 2020-06-08-14:57
 */
@Slf4j
@Service
public class RedisSetUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据key获取set中的所有值
     *
     * @param key
     * @return
     */
    public Optional<Set<String>> get(String key) {
        Set set = redisTemplate.opsForSet().members(key);
        if (null == set) {
            return Optional.empty();
        }
        return Optional.of(set);
    }

    /**
     * 根据value从一个set中查询,是否存在
     *
     * @param key   键
     * @param value 值
     * @return true 存在 false不存在
     */
    public boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将数据放入redis缓存
     *
     * @param key
     * @param values
     * @return
     */
    public long set(String key, Object... values) {
        return redisTemplate.opsForSet().add(key, values);
    }

    /**
     * 将数据放入redis缓存
     * 并设置有效期
     *
     * @param key
     * @param values
     * @return
     */
    public long set(String key, TimeUnit timeUnit, long times, Object... values) {
        Long rst = redisTemplate.opsForSet().add(key, values);
        RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
        boolean b = redisObjectUtils.setExpire(key, timeUnit, times);
        if (b != true) {
            return 0;
        }
        return rst;
    }

    /**
     * 将数据放入redis缓存
     * 并设置有效期
     *
     * @param key
     * @param values
     * @return
     */
    public long set(String group, String key, TimeUnit timeUnit, long times, Object... values) {
        Long rst = redisTemplate.opsForSet().add(group + "_" + key, values);
        RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
        boolean b = redisObjectUtils.setExpire(key, timeUnit, times);
        if (b != true) {
            return 0;
        }
        return rst;
    }

    /**
     * 获取set的长度
     *
     * @param key
     * @return
     */
    public long getSize(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    /**
     * 根据key获取redis的值
     *
     * @param key
     * @return
     */
    public Optional<Object> get(String group, String key) {
        Object s = redisTemplate.opsForValue().get(RedisConstant.REDIS_SET + "_" + group + "_" + key);
        if (null == s) {
            return Optional.empty();
        }
        return Optional.of(s);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String group, String key) {
        redisTemplate.delete(RedisConstant.REDIS_SET + "_" + group + "_" + key);
    }

    /**
     * 根据key删除redis
     *
     * @param group
     */
    public void delgroup(String group) {
        //最后一定要带上 *
        Set<String> keys = redisTemplate.keys(RedisConstant.REDIS_SET + "_" + group + "_*");
        redisTemplate.delete(keys);
    }

    /**
     * 返回移除值得个数
     *
     * @param key
     * @param values
     * @return
     */
    public long del(String key, Object... values) {
        Long rst = redisTemplate.opsForSet().remove(key, values);
        return rst;
    }
}
